<!--
 * @Author: ye_zhiguo
 * @Date: 2021-07-09 09:34:32
 * @LastEditTime: 2021-07-29 15:26:21
 * @Description: 抄录数据
 * @FilePath: \androidd:\PMS-APP\src\modules\tour\pages\tourTask\copyData.vue
-->
<template>
    <article class="bg_4dbfae excuse" id="bgcfff">
        <van-nav-bar title="抄录设备数据" left-text="" left-arrow @click-left="back" fixed>
            <template slot="right">
                <div class="parent-circle">
                    <div class="child-circle"></div>
                </div>
            </template>
        </van-nav-bar>
        <van-collapse>
            <div class="line-flex">
                <div>
                    <span class="line_green"></span>
                    <span>{{deviceInfo.deviceName}}</span>
                </div>
                <div class="sureBtn" @click="update">
                    <img src="../../images/u2360.png" style="width:25px;height:25px;">
                    <span>同步数据</span>
                </div>
            </div>
            <div class="van-collapse-item__content" v-for="(item,i1) in copyChildConfig" :key="i1">
                <!-- <div v-if="item.copyName !='本次动作次数' && item.copyName !='累计动作次数' && item.copyName !='泄漏电流最大值' && item.copyName !='表盘最大值'"> -->
                    <div class="line-title font14">{{item.copyName}}</div>
                    <div>
                        <div class="line">
                            <van-field v-model="item.copyValue" type="number" maxlength="5" label="查看历史" input-align="right" :placeholder="'请输入'+item.copyName">
                                <div slot="label">
                                    <span @click="showHistory(item,i1)">查看历史</span>
                                </div>
                            </van-field>
                        </div>
                    </div>
                    <!-- <div class="wrapper" v-show='value == item.copyName'>
                        <div class="block">
                            <div class="historyItem" v-for="(item1,i2) in historyData" :key="i2">
                                <div>{{item1.createTime | creatTime}} </div>
                                <div>{{item1.copyValue}} </div>
                            </div>
                        </div>
                    </div> -->
                <!-- </div> -->
                <!-- <div class="noHistory" v-if="item.copyName =='本次动作次数' || item.copyName =='累计动作次数' || item.copyName =='泄漏电流最大值' || item.copyName =='表盘最大值'">
                    <div class="line">
                        <van-field v-model="item.copyValue" readonly :label="item.copyName" input-align="right" >
                        </van-field>
                    </div>
                </div> -->
            </div>
            <!-- 历史数据弹窗 -->
            <van-popup v-model="show" position="bottom" style="height:320px;">
                <div class="popup-title">查看历史</div>
                <van-radio-group v-model="radio">
                    <div>
                        <van-row>
                            <van-col span="10">时间</van-col>
                            <van-col span="8">{{copyName}}</van-col>
                            <van-col span="6"></van-col>
                        </van-row>
                        <div style="height:170px;" >
                            <van-row v-for="(item1,i2) in historyData" :key="i2" @click="getCopyValue1(item1,i2)">
                                <van-col span="10">{{item1.createTime | creatTime}}</van-col>
                                <van-col span="8">{{item1.copyValue}}</van-col>
                                <van-radio  :name="i2" />
                            </van-row>
                        </div>
                    </div>
                </van-radio-group>
                <div class="btnBox">
                    <div class="flex">
                        <div class="noPass" @click="colsePopup">取消</div>
                        <div class="pass" @click="confirm()">确定</div>
                    </div>
                </div>
            </van-popup>
            <div class="footPart">
                <van-button class="sureBtn" size="small" @click="sumbit">完成</van-button>
            </div>
        </van-collapse>
    </article>
</template>

<script type="text/javascript">
    import Utils from '@/utils/util'
    import { addCopyRecord , submitCopyData, getHistoryIInfo, getChildConfigList, getCopyConfigList , getCopyValue } from '../../api/dataCopy';
    export default {
        name: 'copyData',
        components: {},
        data() {
            return {
                // value: '',    //是否显示历史记录
                copyChlidList:[],   //抄录子项  
                copyChildConfig: [], //获取的抄录子项数据
                //登录人的信息
                userInfo: JSON.parse(localStorage.getItem('userInfo')),
                deviceType: "",  //设备类型
                substationId: "",  //变电站ID
                deviceInfo: {},//设备信息
                taskInfo: {},//任务信息
                historyData:[], //历史信息
                historyValue:[], //历史抄录值
                show:false,
                copyName:'',    //选中的子项名字
                valueBake:'', //选中的抄录值
                indexBake:'', //选中的抄录子项下标
                copyValueList:[],    //抄录子项的上一次抄录值列表
                radio:'',
            }
        },
        methods: {
            /**
             * @description: 返回
             */
            back() {
                if (this.taskInfo.id) {
                    this.$router.push({
                        name: "executeIndex",
                        params: {
                            taskInfo: this.taskInfo
                        }
                    })
                } else {
                    history.back(-1)
                }

            },

            /**
             * @description: 初始化
             */
            init() {
                // this.value = ''
                this.copyChildConfig = []
            },
            /**
             * @description: 重置选择历史数据
             */
            initHistory() {
               this.indexBake = ''
               this.valueBake = ''
               this.radio = ''
               this.selectValue = ''
            },
            /**
             * @description: 提交
             */
            sumbit() {
                 var self = this;
                //输入验证
                    for(let j=0;j<self.copyChildConfig.length;j++){
                        if(self.copyChildConfig[j].copyValue && self.copyChlidList[j].copyRangeMaxmum && self.copyChlidList[j].copyRangeMinimum){
                            if(self.copyChildConfig[j].copyValue<parseInt(self.copyChlidList[j].copyRangeMinimum) || self.copyChildConfig[j].copyValue>parseInt(self.copyChlidList[j].copyRangeMaxmum)){
                                return self.$Toast.fail(self.copyChildConfig[j].copyName+'\n'+'超出范围')
                            }
                        }
                    }
                //空值采用上一次的抄录值
                for(let i=0;i<self.copyChildConfig.length;i++){
                    if(!self.copyChildConfig[i].copyValue){
                        self.copyChildConfig[i].copyValue = self.copyValueList[i].copyValue
                    }
                }
                console.log(self.copyChildConfig)
                // 提交的数据
                var params = {
                    copyPerson: self.userInfo.userName,
                    copyPersonId: self.userInfo.userId,
                    copyTime: Date.now(),
                    deviceDataList: self.copyChildConfig,
                    deviceType: self.deviceInfo.deviceTypeId,
                    deviceTypeName:self.deviceInfo.deviceTypeName,
                    roleCode:'',
                    substationId: self.taskInfo.substationId,
                    substationName: self.taskInfo.substationName,
                    userId: self.userInfo.userId
                };
                // console.log(params)
                console.log(params)
                // params.requireFinishTime = self.currentDate;
                self.$Toast.loading({
                    duration: 0,//展示时长 0不会消失
                    message: "抄录数据提交中...",
                    forbidClick: true,//是否禁止背景点击
                    overlay: true//是否显示背景遮罩层
                })
                addCopyRecord(params).then(res => {
                    self.$Toast.clear();
                    if (res.success) {
                        self.back()
                    } else {
                        self.$Toast.fail(res.msg)
                    }
                }).catch(err => {
                    self.$Toast.fail(err)
                })
            },

            /**
             * @description: 更新
             */
            update() {
                this.getCopyConfig()
            },

            /**
            * @description: 获取抄录项
            */
            getCopyConfig() {
                var self = this;
                self.init()
                // 提交的数据
                var params = {
                    deviceType: self.deviceType,
                    pageNum: '1',
                    pageSize: '10',
                    roleCode: "",
                    substationId: self.substationId,
                    userId: self.userInfo.userId,
                };
                self.$Toast.loading({
                    message: '加载中...',
                    forbidClick: true,
                });
                getCopyConfigList(params).then(res => {
                    if (res.success) {
                        if (res.data.length == 0) return self.$Toast.fail('暂无抄录项');
                        self.getChildConfig(res.data[0].copyConfigId)
                        self.$Toast.success('加载成功');
                    } else {
                        self.$Toast.fail(res.msg);
                    }
                }).catch(err => {
                    self.$Toast.fail('获取抄录项失败');
                })
            },
             /**
            * @description: 获取抄录子项上一次的抄录值
            */
            getValue(e,i) {
                var self = this;
                var parmas={
                    "copyCode": e.copyCode,
                    "deviceId": self.deviceInfo.deviceId,
                    "roleCode": "",
                    "userId": self.userInfo.userId
                }
                getCopyValue(parmas).then(res => {
                    if (res.success) {
                        console.log(res.data)
                        self.copyValueList[i]={
                            'copyValue':res.data.copyValue
                        }
                        e.copyValue =res.data.copyValue
                    } else {
                        self.$Toast.fail(res.msg)
                    }
                }).catch(err => {
                    self.$Toast.fail(err)
                })
            },
            /**
             * @description: 历史数据赋值
             */
            getCopyValue1(i3,index) {
                var self = this;
                self.radio = index
                self.valueBake = i3.copyValue
                // console.log(i3)
            },
            /**
             * @description: 关闭历史弹窗
             */
            colsePopup(){
                var self = this;
                self.show = false
                self.initHistory()
                // console.log('11111')
            },
            /**
             * @description: 确认历史值
             */
            confirm(){
                var self = this;
                // console.log(i2)
                self.copyChildConfig[self.indexBake].copyValue = self.valueBake?self.valueBake:self.copyChildConfig[self.indexBake].copyValue
                self.initHistory()
                // console.log(self.submitCopyData)
                self.show = false
            },
            /**
            * @description: 获取抄录子项
            */
            getChildConfig(e) {
                var self = this;
                // 提交的数据
                var params = {
                    copyItemConfigId: e,
                    pageNum: '1',
                    pageSize: '10',
                    roleCode: "",
                    userId: self.userInfo.userId
                };
                getChildConfigList(params).then(res => {
                    if (res.success) {
                        if (res.data.length == 0){
                            return self.$Toast.fail('暂无抄录子项');
                        }
                        self.copyChlidList = res.data;
                        self.copyChildConfig = [];
                        for(let i=0;i<res.data.length;i++) {
                            let obj={
                                copyCode:res.data[i].copyCode,
                                copyName:res.data[i].copyName,
                                copyValue:"",
                                deviceId:self.deviceInfo.deviceId,
                                deviceName:self.deviceInfo.deviceName,
                            }
                            // self.$set(self.copyChildConfig[i], "copyValue", '')
                            self.copyChildConfig.push(obj)
                            self.getValue(self.copyChildConfig[i],i)
                        }
                    } else {
                        self.$Toast.fail(res.msg);
                    }
                }).catch(err => {
                    self.$Toast.fail('获取抄录子项失败');
                })
            },
            /**
             * @description: 获取历史数据
             */
            showHistory(e,i) {
                var self = this
                var info = {
                    copyCode: e.copyCode,
                    roleCode: "",
                    userId: self.userInfo.userId,
                    deviceId: self.deviceInfo.deviceId,
                    deviceName: self.deviceInfo.deviceName,
                };
                //获取历史数据
                getHistoryIInfo(info).then(res => {
                    if (res.success) {
                        if (res.data.length == 0){
                            return self.$Toast.fail('暂无历史数据');
                        } 
                        self.historyData = res.data
                        self.copyName = e.copyName
                        self.indexBake = i
                        self.show = true
                    } else {
                        self.$Toast.fail(res.msg);
                    }
                }).catch(err => {
                    self.$Toast.fail('获取历史失败');
                })
            },
        },
        created() {
        },
        mounted() {
            var data = {
                type: "1",
                taskInfo: this.$route.query.taskInfo,
                deviceInfo: this.$route.query.deviceInfo
            }
            if (this.$route.query.deviceInfo.deviceTypeId == '0501001') {
                return this.$router.push({
                    name: "TourBatteryCopy",
                    params:data
                })
            }
            this.taskInfo = this.$route.query.taskInfo;//任务详情
            this.deviceInfo = this.$route.query.deviceInfo;//设备信息
            if (this.taskInfo && this.deviceInfo) {
                this.deviceType = this.deviceInfo.deviceTypeId; //设备类型
                this.substationId = this.taskInfo.substationId;  //变电站ID
            }
            console.log(this.deviceInfo)
            console.log(this.taskInfo)
            this.getCopyConfig()
        },
        filters: {
            creatTime(e) {
                return Utils.DateUtil.getFormatDateByLong(e, 'yyyy-MM-dd hh:mm')
            }
        }
    }
</script>

<style scoped>
    .line-title {
        height: 30px;
        line-height: 30px;
        padding-left: 14px;
    }

    /deep/ .van-collapse {
        padding-top: 80px;
        height: calc(100vh - 131px);
        overflow-y: auto;
    }

    .line-flex {
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 1rem !important;
        padding: 0 14px;
    }

    /* .wrapper {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100%;
        background: #F5F6FA !important;
        overflow: auto;
    }

    .block {
        width: 100%;
        max-height: 120px;
        padding: 0 40px;
        border-radius: 5px;
    }

    .historyItem {
        display: flex;
        justify-content: space-between;
        height: 40px;
        line-height: 40px;
        border-bottom: 1px solid #ECECEC;
    } */

    .footPart {
        width: 100%;
        height: 50px;
        position: fixed;
        bottom: 0;
        left: 0;
        border-top: 1px solid #efefef;
        background: #fff;
        text-align: center;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .footPart .van-button {
        width: 100px;
        margin: 0 10px;
        font-size: 15px;
    }

    .sureBtn {
        background: #17b49b;
        color: #fff;
        padding: 5px;
        border-radius: 5px;
        display: flex;
        width: 110px;
        justify-content: space-around;  
    }
    .popup-title{   
        font-size: 1.125rem;
        line-height: 24px;
        background: rgb(23, 180, 155);
        color: rgb(255, 255, 255);
        text-align: center;
        padding: 12px
    }
    .van-col{
        font-size: 1rem;
        color: #333333;
        text-align: center;
        padding: 10px;
    }
    .van-row{
        margin: 0 16px 8px 16px;
        border-bottom: 1px solid #ccc;
    }
    .van-radio{
        justify-content: space-around;
        padding-top: 10px;
    }
    ::after {
        border: 0;
    }

    #bgcfff{
        background-color: #fff;
        height: 100%;
    }
    #bgcfff .van-collapse-item__content{
        padding-bottom: 0;
    }
    /* #bgcfff .noHistory .line /deep/ .van-field__label{
        background:#fff;
        color:#969799;
        text-align:left;
        width: 7em;
        padding: 0;
    } */
    .line {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .line /deep/ .van-field__label{
        background: #1481EE;
        color: white;
        text-align: center;
        border-radius: 3px;
        padding: 5px 10px;
    }
    .btnBox {
        padding-bottom: 20px;
    }
    .flex {
        width: 40%;
        margin: 0 auto;
        display: flex;
        justify-content: space-around;
        align-items: center;
    }
    .flex>div {
        width: 6rem;
        height: 2rem;
        line-height: 2rem;
        text-align: center;
        border-radius: 8px;
    }
    .noPass {
        background-color: #fff;
        text-align: center;
        color: #000;
        margin-right: 0.5rem;
        box-sizing: border-box;
        border-width: 1px;
        border-style: solid;
        border-color: rgba(51, 51, 51, 1);
    }

    .pass {
        color: #fff;
        margin-left: 0.5rem;
        background: inherit;
        background-color: rgb(24, 181, 156);
        border: none;
    }
</style>